// 移动端交互脚本

// 显示提示信息
function showToast(message) {
    // 创建提示元素
    const toast = document.createElement('div');
    toast.className = 'toast';
    toast.textContent = message;
    
    // 设置样式
    toast.style.position = 'fixed';
    toast.style.top = '50%';
    toast.style.left = '50%';
    toast.style.transform = 'translate(-50%, -50%)';
    toast.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
    toast.style.color = 'white';
    toast.style.padding = '10px 20px';
    toast.style.borderRadius = '20px';
    toast.style.fontSize = '14px';
    toast.style.zIndex = '1000';
    toast.style.opacity = '0';
    toast.style.transition = 'opacity 0.3s';
    
    // 添加到页面
    document.body.appendChild(toast);
    
    // 显示动画
    setTimeout(() => {
        toast.style.opacity = '1';
    }, 10);
    
    // 自动隐藏
    setTimeout(() => {
        toast.style.opacity = '0';
        setTimeout(() => {
            document.body.removeChild(toast);
        }, 300);
    }, 2000);
}

// 添加触摸反馈效果
document.addEventListener('DOMContentLoaded', function() {
    // 为所有可点击元素添加触摸反馈
    const clickables = document.querySelectorAll('.action-item, .task-card, .menu-item, .tab-item, .btn');
    
    clickables.forEach(element => {
        element.addEventListener('touchstart', function() {
            this.style.opacity = '0.7';
        });
        
        element.addEventListener('touchend', function() {
            this.style.opacity = '1';
        });
    });
    
    // 防止双击缩放
    let lastTouchEnd = 0;
    document.addEventListener('touchend', function(event) {
        const now = Date.now();
        if (now - lastTouchEnd <= 300) {
            event.preventDefault();
        }
        lastTouchEnd = now;
    }, false);
});

// 模拟数据加载
function simulateDataLoading() {
    // 显示加载中提示
    showToast('数据加载中...');
    
    // 模拟网络延迟
    setTimeout(() => {
        showToast('数据加载完成');
    }, 1000);
}

// 页面切换动画
function switchPage(fromPage, toPage) {
    // 添加过渡动画
    const fromElement = document.querySelector(fromPage);
    const toElement = document.querySelector(toPage);
    
    if (fromElement && toElement) {
        // 设置初始状态
        toElement.style.transform = 'translateX(100%)';
        toElement.style.transition = 'transform 0.3s ease-out';
        
        // 执行动画
        setTimeout(() => {
            fromElement.style.transform = 'translateX(-100%)';
            fromElement.style.transition = 'transform 0.3s ease-out';
            toElement.style.transform = 'translateX(0)';
        }, 10);
    }
}

// 下拉刷新功能
function initPullToRefresh() {
    let startY = 0;
    let isPulling = false;
    const pullThreshold = 80; // 下拉阈值
    
    const contentElement = document.querySelector('.content');
    if (!contentElement) return;
    
    contentElement.addEventListener('touchstart', function(e) {
        if (contentElement.scrollTop === 0) {
            startY = e.touches[0].clientY;
            isPulling = true;
        }
    });
    
    contentElement.addEventListener('touchmove', function(e) {
        if (!isPulling) return;
        
        const currentY = e.touches[0].clientY;
        const diff = currentY - startY;
        
        if (diff > 0 && diff < pullThreshold * 2) {
            contentElement.style.transform = `translateY(${diff * 0.5}px)`;
        }
    });
    
    contentElement.addEventListener('touchend', function() {
        if (!isPulling) return;
        
        isPulling = false;
        contentElement.style.transform = '';
        contentElement.style.transition = 'transform 0.3s ease-out';
        
        // 如果下拉超过阈值，触发刷新
        if (parseInt(contentElement.style.transform.replace(/[^\d-]/g, '')) > pullThreshold) {
            simulateDataLoading();
        }
    });
}

// 初始化页面
document.addEventListener('DOMContentLoaded', function() {
    // 初始化下拉刷新
    initPullToRefresh();
    
    // 模拟初始数据加载
    setTimeout(() => {
        simulateDataLoading();
    }, 500);
});

// 添加长按事件
function addLongPress(element, callback) {
    let pressTimer;
    let isLongPress = false;
    
    element.addEventListener('touchstart', function(e) {
        pressTimer = setTimeout(() => {
            isLongPress = true;
            callback(e);
        }, 800); // 长按800毫秒触发
    });
    
    element.addEventListener('touchend', function() {
        clearTimeout(pressTimer);
    });
    
    element.addEventListener('touchmove', function() {
        clearTimeout(pressTimer);
    });
}

// 添加滑动手势
function initSwipeGestures() {
    let startX = 0;
    let startY = 0;
    let isMoving = false;
    
    document.addEventListener('touchstart', function(e) {
        startX = e.touches[0].clientX;
        startY = e.touches[0].clientY;
        isMoving = true;
    });
    
    document.addEventListener('touchmove', function(e) {
        if (!isMoving) return;
        
        const currentX = e.touches[0].clientX;
        const currentY = e.touches[0].clientY;
        const diffX = currentX - startX;
        const diffY = currentY - startY;
        
        // 检测水平滑动
        if (Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > 50) {
            // 向右滑动
            if (diffX > 0) {
                // 可以添加返回上一页的逻辑
            }
            // 向左滑动
            else {
                // 可以添加进入下一页的逻辑
            }
            
            isMoving = false;
        }
    });
    
    document.addEventListener('touchend', function() {
        isMoving = false;
    });
}

// 初始化滑动手势
document.addEventListener('DOMContentLoaded', function() {
    initSwipeGestures();
});

// 添加振动反馈（如果设备支持）
function vibrate(duration = 50) {
    if ('vibrate' in navigator) {
        navigator.vibrate(duration);
    }
}

// 为按钮添加振动反馈
document.addEventListener('DOMContentLoaded', function() {
    const buttons = document.querySelectorAll('.btn, .action-item, .menu-item');
    
    buttons.forEach(button => {
        button.addEventListener('touchstart', function() {
            vibrate(10); // 轻微振动
        });
    });
});